﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.DiemThuongKhachHang
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            HienThi();
        }

        void HienThi()
        {
            hienthi = Common.StaticVariable.InterfaceObject.SelectTinhDiemThuong();
            dgvHienThi.DataSource = hienthi;
            fix();
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.DiemThuongKhachHang.DiemDaDung].Visible = true;
            dgvHienThi.Columns[Common.Constants.DiemThuongKhachHang.MaKhachHang].Visible = true;
            dgvHienThi.Columns[Common.Constants.DiemThuongKhachHang.TenKhachHang].Visible = true;
            dgvHienThi.Columns[Common.Constants.DiemThuongKhachHang.TongDiem].Visible = true;
            dgvHienThi.Columns[Common.Constants.DiemThuongKhachHang.DiemDaDung].HeaderText = "Điểm Đã Dùng";
            dgvHienThi.Columns[Common.Constants.DiemThuongKhachHang.MaKhachHang].HeaderText = "Mã Khách Hàng";
            dgvHienThi.Columns[Common.Constants.DiemThuongKhachHang.TenKhachHang].HeaderText = "Tên Khách Hàng";
            dgvHienThi.Columns[Common.Constants.DiemThuongKhachHang.TongDiem].HeaderText = "Tổng Điểm";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Khởi tạo
        bool checkClose = false;
        Entities.DiemThuongKhachHang diemthuongkhachhang;
        Entities.DiemThuongKhachHang[] hienthi;
        Entities.TyLeTinh[] tyletinh;

        public frmQuanLy()
        {
            InitializeComponent();
            try
            {
                HienThi();
                tyletinh = Common.StaticVariable.InterfaceObject.SelectTyLeTinh();
                lbTyLeTinh.Text = "0";
                if (tyletinh.Length != 0)
                {
                    lbTyLeTinh.Text = tyletinh[tyletinh.Length - 1].SoTien;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Check Permission
        public bool CheckPer(int hanhDong)
        {
            return Common.Utilities.Check(Common.FormName.QuanLyTinhDiemThuong, hanhDong);
        }
        #endregion

        #region thêm
        void Them()
        {
            try
            {
                // check per
                if (!CheckPer(1))
                {
                    return;
                }

                frmXuLy frm = new frmXuLy();
                frm.GetData = new GUI.DiemThuongKhachHang.frmXuLy.GetString(GetValue);
                
                frm.Show();

                HienThi();
                tyletinh = Common.StaticVariable.InterfaceObject.SelectTyLeTinh();
                lbTyLeTinh.Text = "0";
                if (tyletinh.Length != 0)
                {
                    lbTyLeTinh.Text = tyletinh[tyletinh.Length - 1].SoTien;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region tạo thẻ
        void TaoThe()
        {
            try
            {
                // check per
                if (!CheckPer(1))
                {
                    return;
                }
                if (i < 0)
                {
                    return;
                }
                if (dgvHienThi.RowCount == 0)
                {
                    return;
                }
                diemthuongkhachhang =((Entities.DiemThuongKhachHang[])dgvHienThi.DataSource)[i];
                // check object
                if (diemthuongkhachhang == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }

                GUI.TheVip.frmXuLy frm = new TheVip.frmXuLy(diemthuongkhachhang);
                frm.GetData = new GUI.TheVip.frmXuLy.GetString(GetValue);
                
                frm.Show();
                HienThi();

            }
            catch
            {
            }
        }
        #endregion

        #region tìm kiếm
        public void TimKiem()
        {
            if (rdoMa.Checked)
            {
                dgvHienThi.DataSource = (Entities.DiemThuongKhachHang[])hienthi.Where(p => p.MaKhachHang.ToUpper().IndexOf(txtTimKiem.Text.ToUpper())>=0).ToArray();
                fix();
            }
            if (rdoTen.Checked)
            {
                dgvHienThi.DataSource = (Entities.DiemThuongKhachHang[])hienthi.Where(p => p.TenKhachHang.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
                fix();
            }
        }
        #endregion

        #region thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Event
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            btnTaoTheVip.Click += new EventHandler(btnTaoTheVip_Click);
            btnThem.Click +=new EventHandler(btnThem_Click);
            btnThoat.Click +=new EventHandler(btnThoat_Click);
            btnTim.Click += new EventHandler(btnTim_Click);
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            this.FormClosing +=new FormClosingEventHandler(frmQuanLy_FormClosing);
            this.WindowState = FormWindowState.Maximized;
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }
        int i = 0;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        void btnTaoTheVip_Click(object sender, EventArgs e)
        {
            TaoThe();
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            Them();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        #endregion
    }
}
#endregion